package thread;

/**
 * 实现Runnable接口的同步方法
 *
 * @author Bboy Dday
 * @create 2020:06:14--13:38
 */
class Windows3 implements Runnable{

    private int ticket = 100;
    Object obj = new Object();
    boolean flag = true;

    @Override
    public void run() {

        while ( flag ) {
            show();
        }
    }

    public synchronized void show(){

        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + "售票 : " + ticket);
            ticket--;
        }else flag = false;

    }

}

public class WindowTest3 {
    public static void main(String[] args) {

        Windows3 windows = new Windows3();
        Thread t1 = new Thread( windows );
        Thread t2 = new Thread( windows );
        Thread t3 = new Thread( windows );

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }

}